import 'package:flutter/material.dart';
import 'package:hugeicons/hugeicons.dart';
import 'package:provider/provider.dart';
import 'package:tool/utils/DateTimeUtils.dart';

import '../../base/base_widget.dart';
import '../../resources.dart';
import 'vm/home_model.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<StatefulWidget> createState() => _HomeState();
}

class _HomeState extends BaseWidgetState<HomeModel, HomePage> {
  @override
  bool get wantKeepAlive => true;

  @override
  HomeModel initModel() {
    return HomeModel();
  }

  @override
  Widget bodyWidget(BuildContext context) {
    return Consumer<HomeModel>(
        builder: (BuildContext context, model, Widget? child) {
      return Column(
        children: [
          commonTitleBar(word.text_title,
              onTap: model.showDetail
                  ? () {
                      model.changeShowDetail(false);
                    }
                  : null),
          Expanded(
            child: model.showDetail
                ? _buildDetail()
                : Stack(
                    children: [
                      Positioned.fill(
                        child: _buildList(),
                      ),
                      Positioned(
                        right: 15.ss,
                        bottom: 15.ss,
                        child: _buildAdd(),
                      ),
                    ],
                  ),
          ),
        ],
      );
    });
  }

  Widget _buildList() {
    if (model.notes.isEmpty) {
      return Center(child: Text(word.text_empty, style: normal016));
    }
    return ListView.builder(
        padding: EdgeInsets.all(12.ss),
        itemCount: model.notes.length,
        itemBuilder: (context, index) {
          var detailModel = model.notes[index];
          return Container(
            margin: EdgeInsets.only(bottom: 12.ss),
            child: InkWell(
              onTap: () {
                model.currentIndex = index;
                model.changeShowDetail(true);
              },
              onLongPress: () {
                Toast.showDialogWidget(
                    child: Container(
                  width: (designWidth * 0.7).ss,
                  padding: EdgeInsets.symmetric(vertical: 5.ss),
                  decoration: BoxDecoration(
                      color: ColorUtils.color_FFFFFF,
                      borderRadius: BorderRadius.circular(15.ss)),
                  child: Column(
                    children: [
                      InkWell(
                        onTap: () {
                          Nav.pop();
                          //分享弹窗
                        },
                        child: Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(10.ss),
                          alignment: Alignment.center,
                          child: Text('分享', style: normal314),
                        ),
                      ),
                      const Divider(color: ColorUtils.color_F5F5F5),
                      InkWell(
                        onTap: () {
                          Nav.pop();
                          model.remove(index);
                        },
                        child: Container(
                          width: double.infinity,
                          padding: EdgeInsets.all(10.ss),
                          alignment: Alignment.center,
                          child: Text('删除', style: normal314),
                        ),
                      ),
                    ],
                  ),
                ));
              },
              child: Container(
                padding:
                    EdgeInsets.symmetric(vertical: 15.ss, horizontal: 15.ss),
                decoration: BoxDecoration(
                    color: ColorUtils.color_F5F5F5,
                    borderRadius: BorderRadius.circular(5.ss)),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('标题：${detailModel.title}',
                        style: normal014,
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis),
                    SizedBox(height: 5.ss),
                    Row(
                      children: [
                        Expanded(
                            child: Text(detailModel.content,
                                style: normal612,
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis)),
                        Text(DateTimeUtils.formatDateMs(detailModel.updateTime),
                            style: normal612),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          );
        });
  }

  Widget _buildAdd() {
    return InkWell(
      onTap: () {
        model.currentIndex = -1;
        model.changeShowDetail(true);
      },
      child: Container(
        width: 38.ss,
        height: 38.ss,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: ColorUtils.app_color.withOpacity(0.6),
            borderRadius: BorderRadius.circular(50.ss),
            border:
                Border.all(color: ColorUtils.color_000000.withOpacity(0.1))),
        child: const HugeIcon(
            icon: HugeIcons.strokeRoundedAdd01,
            color: ColorUtils.color_FFFFFF,
            size: 18),
      ),
    );
  }

  Widget _buildDetail() {
    return SingleChildScrollView(
      padding: EdgeInsets.all(15.ss),
      child: Column(
        children: [
          TextField(
            maxLines: 1,
            maxLength: 15,
            controller: model.titleController,
            focusNode: model.titleFocusNode,
            keyboardType: TextInputType.text,
            style: medium316,
            decoration: InputDecoration(
                counterText: '',
                hintText: word.text_hint,
                border: InputBorder.none),
          ),
          Container(
            alignment: Alignment.centerLeft,
            child: Text(
                DateTimeUtils.formatDateMs(
                    model.currentNoteDetailModel.updateTime),
                style: normal012),
          ),
          TextField(
            maxLines: null,
            controller: model.contentController,
            focusNode: model.contentFocusNode,
            keyboardType: TextInputType.text,
            style: normal314,
            decoration: const InputDecoration(
                counterText: '',
                hintText: '',
                hintStyle: TextStyle(overflow: TextOverflow.fade),
                border: InputBorder.none),
          ),
        ],
      ),
    );
  }
}
